﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DFCLib;
//using CW_Move_Format_Guidelines;
using System.IO;
using System.Data.OleDb;

namespace CW_Format_Guidelines2
{
    public partial class Form1 : Form
    {

        private IDfClientX m_DfClientX = null;
        private schemaFormatGuidelines.FormatGuidelinesDataTable m_FormatGuidelinesTable = new schemaFormatGuidelines.FormatGuidelinesDataTable();


        public Form1()
        {
            InitializeComponent();
        }

        private void m_DoButton_Click(object sender, EventArgs e)
        {

            m_FormatGuidelinesTable.Clear();

            String dqlStr = "SELECT object_name, r_object_id, i_folder_id ";
            dqlStr += "FROM dm_document ";

            //PRODUCTION:
            dqlStr += "WHERE i_cabinet_id = '0c01b1df8000250d' ";

            //DEVELOPMENT:
            //dqlStr += "WHERE i_cabinet_id = '0c0008ae80000b37' ";
            
            dqlStr += "and a_content_type like '%excel%' and i_folder_id IS NOT NULL ";
            dqlStr += "ENABLE(ROW_BASED)";


            m_DfClientX = new DfClientX();

            IDfSessionManager smgr = null;
            IDfClient client = null;
            IDfLoginInfo loginInfo = null;
            IDfQuery query = null;


            try
            {
                client = m_DfClientX.getLocalClient();

                smgr = client.newSessionManager();
                loginInfo = m_DfClientX.getLoginInfo();
                loginInfo.setUser("your user");


                //PRODUCTION:
                loginInfo.setPassword("your password");
                loginInfo.setDomain("CA");

                smgr.setIdentity("DMS", loginInfo);

                IDfSession session = smgr.getSession("DMS");

                query = m_DfClientX.getQuery();
                query.setDQL(dqlStr);

                IDfCollection rs = query.execute(session, (int)tagDfQueryTypes.DF_READ_QUERY);

                int iColCount = rs.getAttrCount();

                while (rs.next())
                {
                    String objName = rs.getString("object_name");
                    IDfId objId = rs.getId("r_object_id");
                    IDfId parentFolderId = rs.getId("i_folder_id");

                    dqlStr = "SELECT r_folder_path FROM dm_folder WHERE r_object_id = '" + parentFolderId.toString() + "' ENABLE(ROW_BASED)";

                    query.setDQL(dqlStr);

                    IDfCollection folderPaths = query.execute(session, (int)tagDfQueryTypes.DF_READ_QUERY);
                    String orgFolderPath = null;

                    while (folderPaths.next())
                    {
                        String currFolderPath = folderPaths.getString("r_folder_path");
                        if (!String.IsNullOrEmpty(currFolderPath))
                        {
                            orgFolderPath = currFolderPath;
                        }

                    }

                    String newFolderPath = null;
                    
                    //PRODUCTION:
                    newFolderPath = orgFolderPath.Replace("Longform Video Content", "Long Form Video Content");

                    //DEVELOPMENT:
                    //newFolderPath = orgFolderPath.Replace("Longform Video Content", "Longform Video Content S4M");

                    //IDfPersistentObject currFormatGuideline = session.getObject(objId);
                    //IDfPersistentObject currObject = session.getObject(objId);
                    String strQualifier = "dm_document WHERE r_object_id = '" + objId.toString() + "'";
                    IDfSysObject currFormatGuideline = (IDfSysObject)session.getObjectByQualification(strQualifier);
                    String errors = null;

                    try
                    {
                        currFormatGuideline.link(newFolderPath);
                        currFormatGuideline.unlink(orgFolderPath);
                        currFormatGuideline.save();
                    }
                    catch (Exception ex)
                    {
                        //MessageBox.Show(ex.ToString());
                        errors = ex.ToString();
                    }

                    schemaFormatGuidelines.FormatGuidelinesRow newRow = m_FormatGuidelinesTable.NewFormatGuidelinesRow();
                    newRow.Name = objName;
                    newRow.ID = objId.toString();
                    newRow.OrgFolderId = parentFolderId.toString();
                    newRow.OrgFolderPath = orgFolderPath;
                    newRow.NewFolderPath = newFolderPath;
                    newRow.Errors = errors;

                    m_FormatGuidelinesTable.AddFormatGuidelinesRow(newRow);

                }


            }
            catch (Exception er)
            {
                MessageBox.Show(er.ToString());
            }


            m_DisplayGrid.DataSource = m_FormatGuidelinesTable;

        }

        private void m_SaveButton_Click(object sender, EventArgs e)
        {
            FileStream excelSheet = new FileStream(@"D:\Bogus\movefileresults.xls", FileMode.CreateNew);
            m_DisplayGrid.ExportToXls(excelSheet);
        }

        private void m_ProcessExcelButton_Click(object sender, EventArgs e)
        {
            m_FormatGuidelinesTable.Clear();

            //Read in Excel worksheet:
            OleDbConnection excelConn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\Bogus\filestomove.xls;Extended Properties=Excel 8.0");
            OleDbDataAdapter excelAdapter = new OleDbDataAdapter("SELECT * FROM FormatGuidelinesTable", excelConn);
            DataTable excelTable = new DataTable();
            excelAdapter.Fill(excelTable);


            m_DfClientX = new DfClientX();

            IDfSessionManager smgr = null;
            IDfClient client = null;
            IDfLoginInfo loginInfo = null;


            try
            {
                client = m_DfClientX.getLocalClient();

                smgr = client.newSessionManager();
                loginInfo = m_DfClientX.getLoginInfo();
                loginInfo.setUser("your user");


                //PRODUCTION:
                loginInfo.setPassword("your password");
                loginInfo.setDomain("CA");

                smgr.setIdentity("DMS", loginInfo);

                IDfSession session = smgr.getSession("DMS");


                foreach (DataRow excelRow in excelTable.Rows)
                {
                    //String objName = excelRow.Field<String>("Name");
                    String objId = excelRow.Field<String>("ID");
                    String orgFolderId = excelRow.Field<String>("Org Folder Path");
                    String newFolderPath = excelRow.Field<String>("new folder path");
                    String newFolderId = excelRow.Field<String>("new folder ID");

                    //newFolderPath = newFolderPath.Substring(8);

                    String strQualifier = "dm_document WHERE r_object_id = '" + objId.ToString() + "'";
                    String strFolderQualifier = "dm_folder WHERE r_object_id='" + orgFolderId.ToString() + "'";
                    IDfSysObject oldFolder = (IDfSysObject)session.getObjectByQualification(strFolderQualifier);
                    String oldFolderPath = ((IDfFolder)oldFolder).getFolderPath(0);
                    IDfSysObject currFormatGuideline = (IDfSysObject)session.getObjectByQualification(strQualifier);
                    String errors = null;

                    strFolderQualifier = "dm_folder WHERE r_object_id='" + newFolderId.ToString() + "'";
                    IDfSysObject newFolder = (IDfSysObject)session.getObjectByQualification(strFolderQualifier);
                    newFolderPath = ((IDfFolder)newFolder).getFolderPath(0);
                    

                    try
                    {
                        currFormatGuideline.link(newFolderPath);
                        currFormatGuideline.unlink(oldFolderPath);
                        currFormatGuideline.save();
                    }
                    catch (Exception ex)
                    {
                        errors = ex.ToString();
                    }

                    schemaFormatGuidelines.FormatGuidelinesRow newRow = m_FormatGuidelinesTable.NewFormatGuidelinesRow();
                    newRow.ID = objId.ToString();
                    newRow.OrgFolderPath = orgFolderId;
                    newRow.NewFolderPath = newFolderPath;
                    newRow.Errors = errors;

                    m_FormatGuidelinesTable.AddFormatGuidelinesRow(newRow);

                }


            }
            catch (Exception er)
            {
                MessageBox.Show(er.ToString());
            }


            m_DisplayGrid.DataSource = m_FormatGuidelinesTable;

        }
    }
}
